package com.yc.hotel.controller;

import java.util.List;


import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yc.hotel.entity.UserInfo;
import com.yc.hotel.entity.UserItem;
import com.yc.hotel.enums.ResultEnum;
import com.yc.hotel.service.IUserItemService;
import com.yc.hotel.util.ConstantInfo;
import com.yc.hotel.vo.ResultVO;

@RestController
@RequestMapping("/userItem")
public class UserItemController {

	@Autowired 
	private IUserItemService userItemService;

	@GetMapping("/findByUno")
	private ResultVO findByUno(HttpSession session) {
		Object obj = session.getAttribute(ConstantInfo.CURRENTMEMBERINFO);
		if (obj == null) {
			return new ResultVO(ResultEnum.LOGIN_ERROR);
		}
		UserInfo user = (UserInfo)obj;
		int uno = user.getUno();
		List<UserItem> userItems = userItemService.findByUno(uno);
		for(UserItem userItem : userItems) {
			String cardid = userItem.getCardid();
			cardid = cardid.substring(0, 6) + "********" + cardid.substring(14,18);
			userItem.setCardid(cardid);
		}
		return new ResultVO(ResultEnum.SUCCESS, userItems);
	}
	@PostMapping("/add")
	private ResultVO add(UserItem userItem, HttpSession session) {
		Object obj = session.getAttribute(ConstantInfo.CURRENTMEMBERINFO);
		if (obj == null) {
			return new ResultVO(ResultEnum.LOGIN_ERROR);
		}
		UserInfo user = (UserInfo)obj;
		int uno = user.getUno();
		userItem.setUno(uno);
		int result = userItemService.add(userItem);
		return new ResultVO(ResultEnum.SUCCESS, result);
	}
}
